//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class SelectionRecord : WorksheetLevelRecord
    {
        public SelectionRecord()
        {
            
        }
        public SelectionRecord(Range range, WorksheetPane pane)
        {
            Contract.Requires(range != null);
            Contract.Requires(pane != null);
            _addresses.CreateItems();
            _row = (ushort)pane.Top;
            _column = (ushort)pane.Left;
            foreach (RangeAddress a in GetAddresses(range))
            {
                _row = (ushort) a.TopLeft.Row;
                _column = (ushort) a.TopLeft.Column;
                _addresses.AddItem((RefU)a);
                
            }
            this._paneType = pane.PaneType;
        }

        private IEnumerable<RangeAddress> GetAddresses(Range range)
        {
            Contract.Assert(range != null);
            if (range is CompositeRange)
            {
                CompositeRange compositeRange = (CompositeRange) range;
                foreach (var range1 in compositeRange.Ranges)
                {
                    yield return range1.Address;
                }
            }
            else
                yield return range.Address;
        }

        //  private readonly List<RangeAddress> addresses = new List<RangeAddress>();
        private RefUs _addresses;
        private ushort _column;
        private PaneType _paneType = PaneType.TopLeft;
        private ushort _row;
        private ushort _irefAct;

        internal override void Apply(BiffReaderWorksheetContext context)
        {
            context.Worksheet.ActiveCell = context.Worksheet[_row, _column];
            var pane = context.Worksheet.EnsurePane(_paneType);
            switch (_paneType)
            {
                case PaneType.TopLeft:
                  //  pane.Top = _row;
                  //  pane.Left = _column;
                    break;
                case PaneType.TopRight:
                //    pane.Left = _column;
                    break;

                case PaneType.BottomLeft:
                //    pane.Top = _row;
                    break;

                case PaneType.BottomRight:
                 //   pane.Top = _row;
                 //   pane.Left = _column;
                    break;
            }
            context.Worksheet.Selection = AddressHelper.ToRange((RangeAddress)_addresses[0], context.Worksheet);
        }


        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.Selection; }
        }

        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            context.Write((byte) _paneType);
            context.Write(_row);
            context.Write(_column);
            context.Write(_irefAct);
//            int offset = 1 + 2 + 2 + 4;
  //          int count = length - offset;
    //        Contract.Assert(count % 6 == 0);
            _addresses.Write(context.Writer);
           /* foreach (var address in addresses)
            {
                AddressHelper.WriteAddres8(address, context.Writer);
            }*/
        }

        protected override void ReadContent(BiffReaderContext context, int length)
        {
            _paneType = (PaneType) context.ReadByte();
            _row = context.ReadUInt16();
            _column = context.ReadUInt16();
            _irefAct = context.ReadUInt16();
            _addresses.Read(context.Reader);
//            unused = reader.ReadUInt32();
  //          int offset = 1 + 2 + 2 + 4;
    //        int count = length - offset;
      //      Contract.Assert(count%6 == 0);
        //    count = count/6;
          //  for (int i = 0; i < count; i++)
            //{
          //      addresses.Add(AddressHelper.ReadAddress8(reader));
         //   }
        }
    }
}